package com.jm.web.builder;


import java.io.IOException;
import java.util.Vector;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * Sep 17, 2011 5:00:45 PM
 */
public class PageBuilder extends BaseBuilder{
	protected String actionType = "outScript";
	protected String actionValue = "2,3,4,5,6,7,8,9"; 
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		actionType = this.baseTag.GetExtValue(BasePage.TAG_ACTION_TYPE,actionType);
		actionValue = this.baseTag.GetExtValue(BasePage.TAG_ACTION_VALUE,actionValue);
	}
	
	
	
	@Override
	public void RenderEnd() {
		if(this.getBaseTag().getPage().isExport())
			return;
		doCmd();
		doAction(this.actionType);
	}
	
	protected void doCmd(){
		Vector<String> cmds = this.getBaseTag().getPage().getCmds();
		if(cmds != null)
		{
			Vector<String> areadyFinshCmd = new Vector<String>();
			for(String cmd : cmds){
				if(BasePage.CMD_TOOLBAR_BUTTON_JS .equalsIgnoreCase(cmd)){
					//註冊工具欄按鍵事件   //2012/03/08 取消 改由toolbarBuilder 直接生成
//					this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_TABS,PageJsGear.getToolbarButtonJs(this.getBaseTag().getPage()));
					areadyFinshCmd.add(cmd);
					continue;
				}else if(BasePage.CMD_TOOLBAR_BUTTON_FILTER_JS.equalsIgnoreCase(cmd)){
					//註冊工具欄Filter按鍵事件
					PageJsGear.registerToolbarFilterJs(this.getBaseTag().getPage());
					areadyFinshCmd.add(cmd);
					continue;
				}
			}
			
			for(String cmd : areadyFinshCmd){
				this.getBaseTag().getPage().getCmds().remove(cmd);
			}
		}
	}
	
	protected void doAction(String action){
		if("outScript".equalsIgnoreCase(action)){
			if(!StringHelper.IsNullOrEmpty(actionValue)){
				String args[] = actionValue.split(",");
				for (int i = 0; i < args.length; i++) {
					onOutScript(Integer.parseInt(args[i]));
				}
			}
			return;
		}else{		
			onCustomAction(action);
		}
		
	}
	
	protected void onCustomAction(String action){
		
	}
	
	
	protected void onOutScript(int lev){
		StringBuilderEx sb = new StringBuilderEx();
		//输出验证的js
		if(lev == BasePage.SCRIPTRUN_VALIDATOR){
				sb.Reset();
				String ruleScript = this.getBaseTag().getPage().outRulesScript();
				String messageScript = this.getBaseTag().getPage().outMessagesScript();
				if(!StringHelper.IsNullOrEmpty(ruleScript) || !StringHelper.IsNullOrEmpty(messageScript)){
					sb.Append("\r\n<script type=\"text/javascript\" charset=\"UTF-8\">");
					//sb.Append("\r\n$(function() {\r\ntry{\r\n");
					
					sb.Append("\r\n if(1){\r\n ");
					sb.Append("var %1$sValidator = $(\"#%1$s\").validate({\r\n",this.getBaseTag().getPage().getDefaultFormID());
					sb.Append(" onkeyup: false");
					sb.Append(",\r\n onblur:false");
					sb.Append(",\r\n onfocusout:false");
					sb.Append(",\r\n onclick:false");
					sb.Append(",\r\n onsubmit:false");
					sb.Append(",\r\n showErrors:$P.showerror2");
					sb.Append(ruleScript);
					sb.Append(messageScript);
					sb.Append("\r\n});");
					sb.Append("\r\n}\r\n ");
					
					sb.Append("$P.validator['%1$s']=%1$sValidator; delete %1$sValidator;\r\n",this.getBaseTag().getPage().getDefaultFormID());
					
					//sb.Append("\r\n}catch(e){alert(e.message);}");
					//sb.Append("\r\n});");
					sb.Append("\r\n</script>\r\n");
				}
		}else if(lev == BasePage.SCRIPTRUN_HTML){
			sb.Reset();
			String strScript = this.getBaseTag().getPage().getReadyScriptSet().get(lev);
			if (StringHelper.Length(strScript) != 0)
			{
				sb.Append(strScript);
				this.getBaseTag().getPage().getReadyScriptSet().remove(lev);
			}
		}else{
			
			if (this.getBaseTag().getPage().getReadyScriptSet() != null)
			{
				sb.Reset();
				String strScript = this.getBaseTag().getPage().getReadyScriptSet().get(lev);
				if (StringHelper.Length(strScript) != 0)
				{
					sb.Append("\r\n<script type=\"text/javascript\" charset=\"UTF-8\">");
					
					
					if(lev > 1)
					{
						//sb.Append("\r\n$(function() {\r\ntry{\r\n");
					}else{
						sb.Append("\r\n	");
					}
					
					if(lev == 1){
						if(this.getBaseTag().getPage().isLogJs()){
							sb.Append("\r\n var time = new Date().getTime();var lg = '';");
						}
					}else{
						sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"lev"+lev));
					}
					
					sb.Append(strScript);
					
					if(lev != 1){
						sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"lev"+lev));
					}
					
					if(lev == 9){
						if(this.getBaseTag().getPage().isLogJs()){
							sb.Append("\r\n alert(lg);");
						}
					}
					
					if(lev > 1)
					{
//						if(this.getBaseTag().getPage().isDebug())
//							sb.Append("\r\n}catch(e){console.log(e);}");
//						else
//							sb.Append("\r\n}catch(e){alert(e.message);}");
//						
//						sb.Append("\r\n});");
					}
					
					
					
					sb.Append("\r\n</script>\r\n");
					this.getBaseTag().getPage().getReadyScriptSet().remove(lev);
				}
			}
		}
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
